package com.xxwy.map;

import com.xxwy.searchtree.BST;
import jdk.management.resource.ThrottledMeter;

/**
 * @author xxwy
 */
public class BSTMap<K extends Comparable, V> implements Map<K, V> {

    private class Node {
        private Node left;
        private Node right;
        private K key;
        private V value;

        public Node(K key, V value, Node left, Node right) {
            this.left = left;
            this.right = right;
            this.key = key;
            this.value = value;
        }

        public Node(K key, V value) {
            this(key, value, null, null);
        }
    }

    private Node root;
    private int size;


    @Override
    public void add(K key, V value) {
        root = add(root, key, value);

    }

    private Node add(Node node, K key, V value) {
        if (root == null) {
            size++;
            return new Node(key, value);
        }
        if (node.key.compareTo(key) < 0) {
            node.left = add(node.left, key, value);
        } else if (node.key.compareTo(key) > 0) {
            node.right = add(node.right, key, value);
        } else {
            node.value = value;
        }
        return node;
    }

    /**
     * 返回key的Node
     *
     * @param node
     * @param key
     * @return
     */
    private Node getNode(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (node.key.compareTo(key) < 0) {
            return getNode(node.left, key);
        } else if (node.key.compareTo(key) > 0) {
            return getNode(node.right, key);
        } else { //same
            return node;
        }
    }


    @Override
    public boolean contains(K key) {
        return getNode(root, key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            return node.value;
        }
        return null;
    }

    @Override
    public void set(K key, V value) {
        Node node = getNode(root, key);
        if (node != null) {
            node.value = value;
        } else {
            throw new IllegalArgumentException(key + " doesn't exist");
        }
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public V remove(K key) {
        Node node = getNode(root, key);
        if (node != null) {
            root = remove(root,key);
            return root.value;
        }
        return null;
    }

    private Node remove(Node node, K key) {
        if(node == null){
            return null;
        }
        if(node.key.compareTo(key) < 0){
            node.left = remove(node.left,key);
            return node;
        }else if(node.key.compareTo(key) >0){
            node.right = remove(node.right,key);
            return node;
        }else {  //当前node
            if(node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            else if(node.right ==null){
                Node leftNode = node.left;
                size--;
                node.left = null;
                return leftNode;
            }
            //左右子树都不为null
            else {
                Node successor = minmum(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.left = node.right = null;
                return successor;
            }
        }

    }

    private Node removeMin(Node node) {
        if(node.left == null){
            node.right = null;
            size--;
            return node.right;
        }
        return removeMin(node.left);

    }

    private Node minmum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minmum(node.left);
    }

}
